/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.login;

import com.farmcode.api.security.AuthenticationAPI;
import com.farmcode.api.security.User;
import com.farmcode.base.web.BaseBacking;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

/**
 *
 * @author Oscar Ivan Prado
 */
@Named(value = "authenticationBackingBean")
@RequestScoped
public class AuthenticationBackingBean extends BaseBacking{

    @EJB
    private AuthenticationAPI authentication;
    
    private boolean authenticationPopup;
    private String userName;
    private String password;
    
    /** Creates a new instance of AuthenticationBackingBean */
    public AuthenticationBackingBean() {
    }
    
    public void authenticate(){
        User user = authentication.authenticate(userName, password);
        if(user.isAuthenticated()){
            getIdentity().setUser(user);
            setAuthenticated(true);
            setAuthenticationPopup(false);
        }else{
            setAuthenticationPopup(true);
        }        
    }

    public void logout(){
        getIdentity().setUser(null);
        setAuthenticated(false);
    }
    
    public void cancel(){
        setAuthenticationPopup(false);
    }
    
    public void initLogin(){
        setAuthenticationPopup(true);
    }
    
    /**
     * @return the authenticationPopup
     */
    public boolean isAuthenticationPopup() {
        return authenticationPopup;
    }

    /**
     * @param authenticationPopup the authenticationPopup to set
     */
    public void setAuthenticationPopup(boolean authenticationPopup) {
        this.authenticationPopup = authenticationPopup;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
}
